package com.jh3502.vivo.function.reimnew;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.jh3502.vivo.R;
import com.jh3502.vivo.function.dialog.CarTypeDialog;
import com.jh3502.vivo.function.dialog.DialogList;
import com.jh3502.vivo.function.dialog.PhotoChooseDialog;
import com.jh3502.vivo.function.reimnew.beans.CategoryInfoBean;
import com.jh3502.vivo.function.reimnew.beans.ClfBean;
import com.jh3502.vivo.function.reimnew.beans.CzcfBean;
import com.jh3502.vivo.function.reimnew.beans.SubBean;
import com.jh3502.vivo.function.reimnew.beans.ZsfBean;
import com.jh3502.vivo.function.travelnew.PhotoChosenAdapter;
import com.jh3502.vivo.utils.Utils;
import com.jh3502.vivo.view.MyRecyclerView;

import java.math.BigDecimal;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

import static com.jh3502.vivo.function.reimnew.ReimNewActivity.FROM_ITEM_CAR_CHOOSE;
import static com.jh3502.vivo.function.reimnew.ReimNewActivity.FROM_ITEM_OUT_CHOOSE;
import static com.jh3502.vivo.function.reimnew.ReimNewActivity.FROM_ITEM_OUT_MONEY_CHOOSE;
import static com.jh3502.vivo.function.reimnew.ReimNewActivity.FROM_ITEM_PHOTO_CHOOSE;

/**
 * author:harisucici on 2018/11/14 11:33
 * email:harisucici@gmail.com
 * name:
 * todo:
 */
public class ReimNewItemAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {


    public static final int CLF = 1;
    public static final int ZSF = 2;
    public static final int CZCF = 3;
    public static final int SUB = 4;
    Context mContext;
    private LayoutInflater layoutInflater;
    private OnRecyclerViewItemClickListener mOnItemClickListener = null;
    private ButtonInterface addReim, choosePlace;
    private CategoryInfoBean datas;
    private int sizeClf;
    private int sizeCzcf;
    private int sizeZsf;
    private int sizeSub;
    private ViewGroup viewGroup;
    private String[] outs = {"否", "是"};
    private String[] moneys = {"50以内", "50以上"};
    private int positionMain;
    private BigDecimal clfBig = new BigDecimal(0), zsfBig = new BigDecimal(0),
            czcfBig = new BigDecimal(0), subBig = new BigDecimal(0);
    private ReimNewItemInterface reimNewItemInterface;
    private double taxi;
    private double hotel;

    public ReimNewItemAdapter(Context context, CategoryInfoBean categoryInfo) {
        this.mContext = context;
        this.datas = categoryInfo;
        layoutInflater = LayoutInflater.from(mContext);
    }

    public void setReimNewItemInterface(ReimNewItemInterface reimNewItemInterface) {
        this.reimNewItemInterface = reimNewItemInterface;
    }

    public void setAddReim(ButtonInterface buttonInterface) {
        this.addReim = buttonInterface;
    }


    public void setChoosePlace(ButtonInterface buttonInterface) {
        this.choosePlace = buttonInterface;
    }

    public void setPositionMain(int position) {
        positionMain = position;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
        this.viewGroup = viewGroup;
        RecyclerView.ViewHolder holder = getViewHolderByViewType(viewType);
        return holder;
    }

    private RecyclerView.ViewHolder getViewHolderByViewType(int viewType) {
        View viewClf = layoutInflater.inflate(R.layout.item_clf, viewGroup, false);
        View viewZsf = layoutInflater.inflate(R.layout.item_zsf, viewGroup, false);
        View viewCzcf = layoutInflater.inflate(R.layout.item_czcf, viewGroup, false);
        View viewSub = layoutInflater.inflate(R.layout.item_sub, viewGroup, false);
        RecyclerView.ViewHolder holder = null;
        switch (viewType) {
            case CLF:
                holder = new ViewHolderClf(viewClf);
                break;
            case ZSF:
                holder = new ViewHolderZsf(viewZsf);
                break;
            case CZCF:
                holder = new ViewHolderCzcf(viewCzcf);
                break;
            case SUB:
                holder = new ViewHolderSub(viewSub);
                break;
            default:
        }
        return holder;
    }

    @Override
    public int getItemViewType(int position) {
        int viewType = -1;
        if (position < sizeClf) {
            viewType = CLF;
        } else if (position > sizeClf - 1 && position < sizeClf + sizeZsf) {
            viewType = ZSF;
        } else if (position > sizeClf + sizeZsf - 1 && position < sizeClf + sizeZsf + sizeCzcf) {
            viewType = CZCF;
        } else if (position > sizeClf + sizeZsf + sizeCzcf - 1 && position < sizeClf + sizeZsf + sizeCzcf + sizeSub) {
            viewType = SUB;
        }


        return viewType;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder viewHolder, final int position) {
        viewHolder.itemView.setTag(position);
        if (position < sizeClf) {
            int pos = position;
            ViewHolderClf vh = (ViewHolderClf) viewHolder;
            vh.type.setText(datas.getClf().get(pos).getTitle());
            if (datas.getClf().get(pos).getAmount() == 0) {
                vh.money.setText("");
            } else {
                vh.money.setText(datas.getClf().get(pos).getAmount() + "");
            }
            vh.money.setOnFocusChangeListener((v, hasFocus) -> {
                if (!hasFocus) {
                    String s = vh.money.getText().toString();
                    if (TextUtils.isEmpty(s)) {
                        return;
                    }
                    datas.getClf().get(pos).setAmount(Double.parseDouble(s));
                    reimNewItemInterface.refreshAdapter(positionMain, getAmountDay(), getOutMoney());

                }
            });
//            vh.money.addTextChangedListener(new TextWatcher() {
//                @Override
//                public void beforeTextChanged(CharSequence s, int start, int count, int after) {
//                }
//
//                @Override
//                public void onTextChanged(CharSequence s, int start, int before, int count) {
//                    if (TextUtils.isEmpty(s)) {
//                        return;
//                    }
//                    datas.getClf().get(pos).setAmount(Double.parseDouble(s.toString()));
//                    reimNewItemInterface.refreshAdapter(positionMain, getAmountDay(),getOutMoney());
//                }
//
//                @Override
//                public void afterTextChanged(Editable s) {
//                }
//            });
            vh.content.setText(datas.getClf().get(pos).getExplain());
            vh.content.addTextChangedListener(new TextWatcher() {
                @Override
                public void beforeTextChanged(CharSequence s, int start, int count, int after) {
                }

                @Override
                public void onTextChanged(CharSequence s, int start, int before, int count) {
                    datas.getClf().get(pos).setExplain(s.toString());
                }

                @Override
                public void afterTextChanged(Editable s) {
                }
            });
            if (datas.getClf().get(pos).getOverTop() == 0) {
                vh.out.setText("否");
                vh.out.setTextColor(Color.parseColor("#FF999999"));
            } else if (datas.getClf().get(pos).getOverTop() == 1) {
                vh.out.setText("是");
                vh.out.setTextColor(Color.parseColor("#FFF36C60"));
            }
            vh.out.setOnClickListener(v -> {
                Intent intent = new Intent(mContext, DialogList.class);
                intent.putExtra("title", "选择是否超标");
                intent.putExtra("list", outs);
                intent.putExtra("positionMain", positionMain);
                intent.putExtra("typeItem", CLF);
                intent.putExtra("positionItem", pos);
                ((Activity) mContext).startActivityForResult(intent, FROM_ITEM_OUT_CHOOSE);
            });
            vh.car.setText(datas.getClf().get(pos).getTraffic());
            vh.car.setOnClickListener(v -> {
                Intent intent = new Intent(mContext, CarTypeDialog.class);
                intent.putExtra("positionMain", positionMain);
                intent.putExtra("typeItem", CLF);
                intent.putExtra("positionItem", pos);
                ((Activity) mContext).startActivityForResult(intent, FROM_ITEM_CAR_CHOOSE);
            });

            StaggeredGridLayoutManager photoManager =
                    new StaggeredGridLayoutManager(4, StaggeredGridLayoutManager.VERTICAL);

            PhotoChosenAdapter mAdapter = new PhotoChosenAdapter(mContext, datas.getClf().get(pos).getFiles());
            mAdapter.setButtonSetOnclickDelete(i -> {
                datas.getClf().get(pos).getFiles().remove(i);
                mAdapter.notifyDataSetChanged();
            });
            vh.photo.setLayoutManager(photoManager);
            vh.photo.setAdapter(mAdapter);

            vh.photoAdd.setOnClickListener(v -> {
                Intent intent = new Intent(mContext, PhotoChooseDialog.class);
                intent.putExtra("positionMain", positionMain);
                intent.putExtra("typeItem", CLF);
                intent.putExtra("positionItem", pos);
                ((Activity) mContext).startActivityForResult(intent, FROM_ITEM_PHOTO_CHOOSE);
            });

        } else if (position > sizeClf - 1 && position < sizeClf + sizeZsf) {
            int pos = position - sizeClf;
            ViewHolderZsf vh = (ViewHolderZsf) viewHolder;
            vh.type.setText(datas.getZsf().get(pos).getTitle());
            if (datas.getZsf().get(pos).getAmount() == 0) {
                vh.money.setText("");
            } else {
                vh.money.setText(datas.getZsf().get(pos).getAmount() + "");
            }
            vh.money.setOnFocusChangeListener((v, hasFocus) -> {
                if (!hasFocus) {
                    String s = vh.money.getText().toString();
                    if (TextUtils.isEmpty(s)) {
                        return;
                    }
                    datas.getZsf().get(pos).setAmount(Double.parseDouble(s));
                    reimNewItemInterface.refreshAdapter(positionMain, getAmountDay(), getOutMoney());

                }
            });
//            vh.money.addTextChangedListener(new TextWatcher() {
//                @Override
//                public void beforeTextChanged(CharSequence s, int start, int count, int after) {
//                }
//
//                @Override
//                public void onTextChanged(CharSequence s, int start, int before, int count) {
//                    if (TextUtils.isEmpty(s)) {
//                        return;
//                    }
//                    datas.getZsf().get(pos).setAmount(Double.parseDouble(s.toString()));
//                    reimNewItemInterface.refreshAdapter(positionMain, getAmountDay(),getOutMoney());
//                }
//
//                @Override
//                public void afterTextChanged(Editable s) {
//                }
//            });

            vh.content.setText(datas.getZsf().get(pos).getExplain());
            vh.content.addTextChangedListener(new TextWatcher() {
                @Override
                public void beforeTextChanged(CharSequence s, int start, int count, int after) {
                }

                @Override
                public void onTextChanged(CharSequence s, int start, int before, int count) {
                    datas.getZsf().get(pos).setExplain(s.toString());
                }

                @Override
                public void afterTextChanged(Editable s) {
                }
            });
            if (datas.getZsf().get(pos).getOverTop() == 0) {
                vh.out.setText("否");
                vh.out.setTextColor(Color.parseColor("#FF999999"));
            } else if (datas.getZsf().get(pos).getOverTop() == 1) {
                vh.out.setText("是");
                vh.out.setTextColor(Color.parseColor("#FFF36C60"));
            }
            vh.out.setOnClickListener(v -> {
                Intent intent = new Intent(mContext, DialogList.class);
                intent.putExtra("title", "选择是否超标");
                intent.putExtra("list", outs);
                intent.putExtra("positionMain", positionMain);
                intent.putExtra("typeItem", ZSF);
                intent.putExtra("positionItem", pos);
                ((Activity) mContext).startActivityForResult(intent, FROM_ITEM_OUT_CHOOSE);
            });

            if (datas.getZsf().get(pos).getStandard() == 1) {
                vh.outMoney.setText(moneys[0]);
                vh.outMoney.setTextColor(Color.parseColor("#FF999999"));
            } else if (datas.getZsf().get(pos).getStandard() == 2) {
                vh.outMoney.setText(moneys[1]);
                vh.outMoney.setTextColor(Color.parseColor("#FFF36C60"));
            } else {
                vh.outMoney.setText("");
            }
            vh.outMoney.setOnClickListener(v -> {
                Intent intent = new Intent(mContext, DialogList.class);
                intent.putExtra("title", "超标额度");
                intent.putExtra("list", moneys);
                intent.putExtra("positionMain", positionMain);
                intent.putExtra("typeItem", ZSF);
                intent.putExtra("positionItem", pos);
                ((Activity) mContext).startActivityForResult(intent, FROM_ITEM_OUT_MONEY_CHOOSE);
            });
            StaggeredGridLayoutManager photoManager =
                    new StaggeredGridLayoutManager(4, StaggeredGridLayoutManager.VERTICAL);

            PhotoChosenAdapter mAdapter = new PhotoChosenAdapter(mContext, datas.getZsf().get(pos).getFiles());
            mAdapter.setButtonSetOnclickDelete(i -> {
                datas.getZsf().get(pos).getFiles().remove(i);
                mAdapter.notifyDataSetChanged();
            });
            vh.photo.setLayoutManager(photoManager);
            vh.photo.setAdapter(mAdapter);

            vh.photoAdd.setOnClickListener(v -> {
                Intent intent = new Intent(mContext, PhotoChooseDialog.class);
                intent.putExtra("positionMain", positionMain);
                intent.putExtra("typeItem", ZSF);
                intent.putExtra("positionItem", pos);
                ((Activity) mContext).startActivityForResult(intent, FROM_ITEM_PHOTO_CHOOSE);
            });
        } else if (position > sizeClf + sizeZsf - 1 && position < sizeClf + sizeZsf + sizeCzcf) {
            int pos = position - sizeClf - sizeZsf;
            ViewHolderCzcf vh = (ViewHolderCzcf) viewHolder;
            vh.type.setText(datas.getCzcf().get(pos).getTitle());
            if (datas.getCzcf().get(pos).getAmount() == 0) {
                vh.money.setText("");
            } else {
                vh.money.setText(datas.getCzcf().get(pos).getAmount() + "");
            }
//            vh.money.addTextChangedListener(new TextWatcher() {
//                @Override
//                public void beforeTextChanged(CharSequence s, int start, int count, int after) {
//                }
//
//                @Override
//                public void onTextChanged(CharSequence s, int start, int before, int count) {
//                    if (TextUtils.isEmpty(s)) {
//                        return;
//                    }
//                    datas.getCzcf().get(pos).setAmount(Double.parseDouble(s.toString()));
//
//                    reimNewItemInterface.refreshAdapter(positionMain, getAmountDay(), getOutMoney());
//                }
//
//                @Override
//                public void afterTextChanged(Editable s) {
//                }
//            });
            vh.money.setOnFocusChangeListener((v, hasFocus) -> {
                if (!hasFocus) {
                    String s = vh.money.getText().toString();
                    if (TextUtils.isEmpty(s)) {
                        return;
                    }
                    datas.getCzcf().get(pos).setAmount(Double.parseDouble(s));
                    reimNewItemInterface.refreshAdapter(positionMain, getAmountDay(), getOutMoney());

                }
            });
            vh.content.setText(datas.getCzcf().get(pos).getExplain());
            vh.content.addTextChangedListener(new TextWatcher() {
                @Override
                public void beforeTextChanged(CharSequence s, int start, int count, int after) {
                }

                @Override
                public void onTextChanged(CharSequence s, int start, int before, int count) {
                    datas.getCzcf().get(pos).setExplain(s.toString());
                }

                @Override
                public void afterTextChanged(Editable s) {
                }
            });
            StaggeredGridLayoutManager photoManager =
                    new StaggeredGridLayoutManager(4, StaggeredGridLayoutManager.VERTICAL);

            PhotoChosenAdapter mAdapter = new PhotoChosenAdapter(mContext, datas.getCzcf().get(pos).getFiles());
            mAdapter.setButtonSetOnclickDelete(i -> {
                datas.getCzcf().get(pos).getFiles().remove(i);
                mAdapter.notifyDataSetChanged();
            });
            vh.photo.setLayoutManager(photoManager);
            vh.photo.setAdapter(mAdapter);

            vh.photoAdd.setOnClickListener(v -> {
                Intent intent = new Intent(mContext, PhotoChooseDialog.class);
                intent.putExtra("positionMain", positionMain);
                intent.putExtra("typeItem", CZCF);
                intent.putExtra("positionItem", pos);
                ((Activity) mContext).startActivityForResult(intent, FROM_ITEM_PHOTO_CHOOSE);
            });
        } else if (position > sizeClf + sizeZsf + sizeCzcf - 1 && position < sizeClf + sizeZsf + sizeCzcf + sizeSub) {
            int pos = position - sizeClf - sizeZsf - sizeCzcf;
            ViewHolderSub vh = (ViewHolderSub) viewHolder;
            vh.type.setText(datas.getSub().get(pos).getTitle());
            if (datas.getSub().get(pos).getAmount() == 0) {
                vh.money.setText("");
            } else {
                vh.money.setText(datas.getSub().get(pos).getAmount() + "");
            }
//            vh.money.addTextChangedListener(new TextWatcher() {
//                @Override
//                public void beforeTextChanged(CharSequence s, int start, int count, int after) {
//                }
//
//                @Override
//                public void onTextChanged(CharSequence s, int start, int before, int count) {
//                    if (TextUtils.isEmpty(s)) {
//                        return;
//                    }
//                    datas.getSub().get(pos).setAmount(Double.parseDouble(s.toString()));
//                    reimNewItemInterface.refreshAdapter(positionMain, getAmountDay(),getOutMoney());
//                }
//
//                @Override
//                public void afterTextChanged(Editable s) {
//                }
//            });
            vh.money.setOnFocusChangeListener((v, hasFocus) -> {
                if (!hasFocus) {
                    String s = vh.money.getText().toString();
                    if (TextUtils.isEmpty(s)) {
                        return;
                    }
                    datas.getSub().get(pos).setAmount(Double.parseDouble(s));
                    reimNewItemInterface.refreshAdapter(positionMain, getAmountDay(), getOutMoney());

                }
            });
            vh.content.setText(datas.getSub().get(pos).getExplain());
            vh.content.addTextChangedListener(new TextWatcher() {
                @Override
                public void beforeTextChanged(CharSequence s, int start, int count, int after) {
                }

                @Override
                public void onTextChanged(CharSequence s, int start, int before, int count) {
                    datas.getSub().get(pos).setExplain(s.toString());
                }

                @Override
                public void afterTextChanged(Editable s) {
                }
            });
            StaggeredGridLayoutManager photoManager =
                    new StaggeredGridLayoutManager(4, StaggeredGridLayoutManager.VERTICAL);

            PhotoChosenAdapter mAdapter = new PhotoChosenAdapter(mContext, datas.getSub().get(pos).getFiles());
            mAdapter.setButtonSetOnclickDelete(i -> {
                datas.getSub().get(pos).getFiles().remove(i);
                mAdapter.notifyDataSetChanged();
            });
            vh.photo.setLayoutManager(photoManager);
            vh.photo.setAdapter(mAdapter);

            vh.photoAdd.setOnClickListener(v -> {
                Intent intent = new Intent(mContext, PhotoChooseDialog.class);
                intent.putExtra("positionMain", positionMain);
                intent.putExtra("typeItem", SUB);
                intent.putExtra("positionItem", pos);
                ((Activity) mContext).startActivityForResult(intent, FROM_ITEM_PHOTO_CHOOSE);
            });
        }


    }

    public void setOnItemClickListener(OnRecyclerViewItemClickListener listener) {
        this.mOnItemClickListener = listener;
    }

    @Override
    public int getItemCount() {
        sizeClf = getListSize(datas.getClf());
        sizeZsf = getListSize(datas.getZsf());
        sizeCzcf = getListSize(datas.getCzcf());
        sizeSub = getListSize(datas.getSub());
        int all = sizeClf + sizeZsf + sizeCzcf + sizeSub;
        return all;
    }

    private int getListSize(List<?> list) {
        if (list == null) {
            return 0;
        }
        return list.size();
    }

    private BigDecimal getAmountDay() {
        for (ClfBean clfBean : datas.getClf()) {
            clfBig = Utils.getInstance().add(clfBig, clfBean.getAmount());
        }
        for (ZsfBean zsfBean : datas.getZsf()) {
            zsfBig = Utils.getInstance().add(zsfBig, zsfBean.getAmount());
        }
        for (CzcfBean czcfBean : datas.getCzcf()) {
            czcfBig = Utils.getInstance().add(czcfBig, czcfBean.getAmount());
        }
        for (SubBean subBean : datas.getSub()) {
            subBig = Utils.getInstance().add(subBig, subBean.getAmount());
        }
        return clfBig.add(zsfBig).add(czcfBig).add(subBig);
    }

    private double getOutMoney() {
        double zsfAll = 0;
        for (ZsfBean zsfBean : datas.getZsf()) {
            zsfAll = Utils.getInstance().add(zsfAll, zsfBean.getAmount());
        }
        double zsfOut = Utils.getInstance().sub(zsfAll, hotel);
        if (zsfOut < 0) {
            zsfOut = 0;
        }


        double czcfAll = 0;
        for (CzcfBean czcfBean : datas.getCzcf()) {
            czcfAll = Utils.getInstance().add(czcfAll, czcfBean.getAmount());
        }
//
        double czcfOut = Utils.getInstance().sub(czcfAll, taxi);
        if (czcfOut < 0) {
            czcfOut = 0;
        }


        return Utils.getInstance().add(zsfOut, czcfOut);
    }

    public void setTaxi(double taxi) {
        this.taxi = taxi;
    }

    public void setHotel(double v) {
        this.hotel = v;
    }

    public interface ButtonInterface {
        void onClick(int position);
    }

    public interface OnRecyclerViewItemClickListener {
        void onItemClick(int i);
    }

    class ViewHolderClf extends RecyclerView.ViewHolder {
        @BindView(R.id.title)
        TextView title;
        @BindView(R.id.type)
        TextView type;
        @BindView(R.id.car)
        TextView car;
        @BindView(R.id.money)
        EditText money;
        @BindView(R.id.out)
        TextView out;
        @BindView(R.id.delete)
        Button delete;
        @BindView(R.id.content)
        EditText content;
        @BindView(R.id.photo_add)
        ImageView photoAdd;
        @BindView(R.id.photo)
        MyRecyclerView photo;

        public ViewHolderClf(View view) {
            super(view);
            ButterKnife.bind(this, view);

        }
    }

    class ViewHolderZsf extends RecyclerView.ViewHolder {
        @BindView(R.id.title)
        TextView title;
        @BindView(R.id.type)
        TextView type;
        @BindView(R.id.money)
        EditText money;
        @BindView(R.id.out)
        TextView out;
        @BindView(R.id.content)
        EditText content;
        @BindView(R.id.out_money)
        TextView outMoney;
        @BindView(R.id.photo_add)
        ImageView photoAdd;
        @BindView(R.id.photo)
        MyRecyclerView photo;

        public ViewHolderZsf(View view) {
            super(view);
            ButterKnife.bind(this, view);

        }
    }

    class ViewHolderCzcf extends RecyclerView.ViewHolder {
        @BindView(R.id.title)
        TextView title;
        @BindView(R.id.type)
        TextView type;
        @BindView(R.id.money)
        EditText money;
        @BindView(R.id.content)
        EditText content;
        @BindView(R.id.photo_add)
        ImageView photoAdd;
        @BindView(R.id.photo)
        MyRecyclerView photo;

        public ViewHolderCzcf(View view) {
            super(view);
            ButterKnife.bind(this, view);

        }
    }

    class ViewHolderSub extends RecyclerView.ViewHolder {
        @BindView(R.id.title)
        TextView title;
        @BindView(R.id.type)
        TextView type;
        @BindView(R.id.money)
        EditText money;
        @BindView(R.id.content)
        EditText content;
        @BindView(R.id.photo_add)
        ImageView photoAdd;
        @BindView(R.id.photo)
        MyRecyclerView photo;

        public ViewHolderSub(View view) {
            super(view);
            ButterKnife.bind(this, view);

        }
    }


}
